﻿using Awsc.DataAcquisitionSys.Core.Common;
using Awsc.DataAcquisitionSys.Core.Models.POCO;


namespace Awsc.DataAcquisitionSys.Core.Databases
{
    public interface IAlarmRuleRepository
    {
        Task<List<AlarmRuleModel>> GetAllRulesAsync();
        Task<bool> AddRuleAsync(AlarmRuleModel model);
        Task<bool> UpdateRuleAsync(AlarmRuleModel model);
        Task<bool> RemoveRuleAsync(int id);
        Task<AlarmRuleModel> GetRuleByName(string name);
        Task<AlarmRuleModel> GetRuleById(int id);

    }
    public class AlarmRuleRepository : IAlarmRuleRepository
    {
        private readonly SQLiteHelper _dbHelper;
        public AlarmRuleRepository(SQLiteHelper dbHelper)
        {
            _dbHelper = dbHelper;
            InitializeDatabase();
        }
        public async Task<List<AlarmRuleModel>> GetAllRulesAsync()
        {
           var result = await _dbHelper.QueryAsync<AlarmRuleModel>("SELECT * FROM AlarmRules");
            return result;
        }

        public async Task<bool> AddRuleAsync(AlarmRuleModel model)
        {
            ArgumentAssert.NotNull(model);
            var insertResult = await _dbHelper.InsertAsync(model);
            return insertResult > 0;
        }

        private void InitializeDatabase()
        {
            // 创建任务元数据表
            var sql = @"
            CREATE TABLE IF NOT EXISTS AlarmRules (
                ID INTEGER PRIMARY KEY AUTOINCREMENT,
                Name TEXT NOT NULL,
                Status INTEGER,
                Level INTEGER,
                RuleType INTEGER,
                IsBuildIn INTEGER,
                AlarmMethods TEXT,
                Condition TEXT,
                Description TEXT,
                Expression TEXT,
                CreateTime REAL,
                UpdateTime REAL
            );
            
            PRAGMA journal_mode = WAL;
            PRAGMA synchronous = NORMAL;
            PRAGMA cache_size = -10000; -- 10MB cache
            PRAGMA temp_store = MEMORY;
            PRAGMA busy_timeout = 3000;";
            _dbHelper.CreateTableAsync(sql).ConfigureAwait(false).GetAwaiter().GetResult();

        }

        public async Task<bool> UpdateRuleAsync(AlarmRuleModel model)
        {
            ArgumentAssert.NotNull(model);
            var updateResult = await _dbHelper.UpdateAsync(model);
            return updateResult > 0;
        }

        public async Task<AlarmRuleModel> GetRuleByName(string name)
        {
            ArgumentAssert.NotNullOrEmpty(name);
            var result = await _dbHelper.FindAsync<AlarmRuleModel>("SELECT * FROM AlarmRules WHERE Name = @Name", new Dictionary<string, object>() { {"Name", name }});
            return result;
        }

        public async Task<AlarmRuleModel> GetRuleById(int id)
        {
            ArgumentAssert.GreaterThanZero(id);
            var result = await _dbHelper.FindAsync<AlarmRuleModel>("SELECT * FROM AlarmRules WHERE Id = @Id", new Dictionary<string, object>() { { "Id", id } });
            return result;
        }

        public async Task<bool> RemoveRuleAsync(int id)
        {
            ArgumentAssert.GreaterThanZero(id);
            var deleteResult = await _dbHelper.ExecuteNonQueryAsync("DELETE FROM AlarmRules WHERE Id = @Id", new Dictionary<string, object>() { { "Id", id } });
            return deleteResult > 0;
        }
    }
}
